﻿using Domain.Config;
using Domain.Event;
using Domain.IRepositorys;
using Domain.Model;
using Domain.Types;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility;

namespace Domain.Services
{
    public class FeeService
    {
        public ITaskRepository taskRepository { get; set; }
        public IUserRepository userRepository { get; set; }
        public IWalletRepository walletRepository { get; set; }
        public IInComeWalletLogRepository inComeWalletLogRepository { get; set; }
        public WalletService walletService { get; set; }

        public void PayUserPropFee(int userID, PropType propType, int num, int zKFee)
        {
            var u = userRepository.GetByKey(userID);

            var firstSuperUser = u.SuperorID > 0 ? userRepository.GetByKey(u.SuperorID) : null;
            var SecondSuperUser = (firstSuperUser != null && firstSuperUser.SuperorID > 0) ? userRepository.GetByKey(firstSuperUser.SuperorID) : null;

            int firstSuperFee = firstSuperUser == null ? 0 : (int)(zKFee * u.GetLevelConfig<double>("一级下线完成任务佣金收益") / 100);
            int secondSuperFee = SecondSuperUser == null ? 0 : (int)(zKFee * u.GetLevelConfig<double>("二级下线完成任务佣金收益") / 100);

            var uWallet = walletRepository.Entities.First(i => i.UserID == u.ID);

            walletService.ChangeInComeWallet(userID, -zKFee,0, zKFee - firstSuperFee - secondSuperFee,
                null,0, FinancialBizType.购买道具, "购买道具-" + propType.ToString(),
                 $"数量:{num} 价格:{zKFee}");

            if (firstSuperUser != null && firstSuperFee > 0)
            {
                walletService.ChangeInComeWallet(firstSuperUser.ID, firstSuperFee,0, 0,
                null, userID, FinancialBizType.购买道具一级提成, "购买道具-" + propType.ToString() + "-一级下线",
                 $"数量:{num} 价格:{zKFee}");
            }

            if (SecondSuperUser != null && secondSuperFee > 0)
            {
                walletService.ChangeInComeWallet(SecondSuperUser.ID, secondSuperFee,0, 0,
                null, userID, FinancialBizType.购买道具二级提成, "购买道具-" + propType.ToString() + "-二级下线",
                 $"数量:{num} 价格:{zKFee}");
            }
        }

        public Dictionary<int, string> HandleTaskUserFee(int auditUserID, TaskAggregate task, TaskTakeAggregate takeTask)
        {
            Dictionary<int, string> map = new Dictionary<int, string>();
            var u = userRepository.GetByKey(takeTask.UserID);

            var firstSuperUser = u.SuperorID > 0 ? userRepository.GetByKey(u.SuperorID) : null;
            var SecondSuperUser = (firstSuperUser != null && firstSuperUser.SuperorID > 0) ? userRepository.GetByKey(firstSuperUser.SuperorID) : null;

            double uTaskPer = u.GetTaskCommissionPer();

            //用户收益
            int uFee = (int)(takeTask.Price * uTaskPer);
            //上级总佣金            
            int totalCommissionFee = (int)(takeTask.Price * u.GetLevelConfig<double>("佣金配置.任务预留佣金百分比") / 100);

            int firstSuperFee = firstSuperUser == null ? 0 : (int)(totalCommissionFee * u.GetLevelConfig<double>("一级下线完成任务佣金收益") / 100);
            int secondSuperFee = SecondSuperUser == null ? 0 : (int)(totalCommissionFee * u.GetLevelConfig<double>("二级下线完成任务佣金收益") / 100);

            var uWallet = walletRepository.Entities.First(i => i.UserID == u.ID);

            walletService.ChangeInComeWallet(takeTask.UserID, uFee,0, -uFee,
                takeTask.ID.ToString(),0, FinancialBizType.完成任务, "完成任务",
                 $"领取任务ID 任务金额:{takeTask.Price} 用户等级:{u.UserLevel.ToString()} 用户完成任务收益比例:{uTaskPer}"
                 );
            map.Add(takeTask.UserID, $"您的任务已审核通过，赚取了{uFee}佣金!");

            if (firstSuperUser != null && firstSuperFee > 0)
            {
                walletService.ChangeInComeWallet(firstSuperUser.ID, firstSuperFee,0, -firstSuperFee,
                    takeTask.ID.ToString(), takeTask.UserID, FinancialBizType.完成任务一级提成, task?.Name,
                     $"领取任务ID 任务金额:{takeTask.Price} 用户等级:{u.UserLevel.ToString()}",
                     JsonConvert.SerializeObject(new { 来源用户ID = takeTask.UserID })); ;
                map.Add(firstSuperUser.ID, $"您的一级下线完成了任务，赚取了{uFee}佣金!");
            }

            if (SecondSuperUser != null && secondSuperFee > 0)
            {
                walletService.ChangeInComeWallet(SecondSuperUser.ID, secondSuperFee,0, -secondSuperFee,
                    takeTask.ID.ToString(), takeTask.UserID, FinancialBizType.完成任务二级提成, task?.Name,
                     $"领取任务ID 任务金额:{takeTask.Price} 用户等级:{u.UserLevel.ToString()}",
                     JsonConvert.SerializeObject(new { 来源用户ID = takeTask.UserID }));
                map.Add(SecondSuperUser.ID, $"您的二级下线完成了任务，赚取了{uFee}佣金!");
            }

            takeTask.IsHandlePay = true;
            takeTask.HandlePayTime = DateTime.Now;
            takeTask.HandleInfo = $"完成任务-领取任务ID 任务金额:{takeTask.Price} 用户等级:{u.UserLevel.ToString()} 用户完成任务收益比例:{uTaskPer} 用户收益:{uFee} 一级上线收入:{firstSuperFee} 二级上线收入:{secondSuperFee}";

            return map;
        }





        public Dictionary<int, string> HandleNewUserReward(int auditUserID, TaskAggregate task, TaskTakeAggregate takeTask)
        {
            Dictionary<int, string> map = new Dictionary<int, string>();
            var u = userRepository.GetByKey(takeTask.UserID);

            u.TaskCount++;

            var uWallet = walletRepository.Entities.First(i => i.UserID == u.ID);

            var fCount = u.GetLevelConfig<int>("新手任务配置.1级任务数");
            var sCount = u.GetLevelConfig<int>("新手任务配置.2级任务数");
            var tCount = u.GetLevelConfig<int>("新手任务配置.3级任务数");
            if (u.TaskCount == fCount)
            {
                AddNewUserTask(u, uWallet, takeTask.ID, u.GetLevelConfig<int>("新手任务配置.1级奖励"), $"完成{fCount}个任务", 0);
                map.Add(takeTask.UserID, $"您完成了新手任务，赚取了{u.GetLevelConfig<int>("新手任务配置.1级奖励")}佣金!");
            }
            if (u.TaskCount == sCount)
            {
                AddNewUserTask(u, uWallet, takeTask.ID, u.GetLevelConfig<int>("新手任务配置.2级奖励"), $"完成{sCount}个任务", 0);
                map.Add(takeTask.UserID, $"您完成了新手任务，赚取了{u.GetLevelConfig<int>("新手任务配置.2级奖励")}佣金!");
            }
            if (u.TaskCount == tCount)
            {
                AddNewUserTask(u, uWallet, takeTask.ID, u.GetLevelConfig<int>("新手任务配置.3级奖励"), $"完成{tCount}个任务", 0);
                map.Add(takeTask.UserID, $"您完成了新手任务，赚取了{u.GetLevelConfig<int>("新手任务配置.3级奖励")}佣金!");
            }

            void AddNewUserTask(UserInfoAggregate u, WalletAggregate uWallet, int takeTaskID, int fee, string Tag,int fromUid)
            {
                walletService.ChangeInComeWallet(u.ID, fee,0, -fee,
                takeTaskID.ToString(), fromUid, FinancialBizType.新手任务, "新手任务",
                 Tag);
            }
            return map;
        }

        public Dictionary<int, string> HandlePromoteTwekveReward(int auditUserID, TaskAggregate task, TaskTakeAggregate takeTask)
        {
            Dictionary<int, string> map = new Dictionary<int, string>();
            var u = userRepository.GetByKey(takeTask.UserID);

            if (u.TaskCount == 1 && u.SuperorID > 0)
            {
                var fuid = u.SuperorID;

                var firstSuperUserWallet = walletRepository.Entities.First(i => i.UserID == fuid);

                var fee = u.GetLevelConfig<int>("十二元现金奖励.好友首次完成任务");

                walletService.ChangeInComeWallet(fuid, fee,0, -fee, takeTask.ID.ToString(), takeTask.UserID, FinancialBizType.十二元现金奖励
                    , "十二元现金奖励", "好友首次完成任务", JsonConvert.SerializeObject(new { 来源用户ID = takeTask.UserID }));

                map.Add(fuid, $"您的下线首次完成任务，赚取了{fee}佣金!");
            }
            return map;
        }
        /// <summary>
        /// 升级后上线拿佣金
        /// </summary>
        /// <param name="auditUserID"></param>
        /// <param name="task"></param>
        /// <param name="takeTask"></param>
        public InComeWalletLogAggregate HandleUserUpSuperorReward(int uid, int upLevelFee)
        {
            var u = userRepository.GetByKey(uid);
            var superorUser = userRepository.GetByKey(u.SuperorID);
            int upPer = superorUser.GetLevelConfig<int>("一级下线升级超级商人收益");

            var inComeFee = upLevelFee * upPer / 100;

            return walletService.ChangeInComeWallet(superorUser.ID, inComeFee,0, -inComeFee, u.ID.ToString(),uid, FinancialBizType.升级超级商人佣金
                    , "升级超级商人", "升级超级商人", JsonConvert.SerializeObject(new { 来源用户ID = u.ID }));
        }
    }
}
